package com.guet.issue.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guet.comment.beans.vo.CommentDateVO;
import com.guet.comment.service.ICommentService;
import com.guet.common.entity.PageDataVO;
import com.guet.common.entity.PageVO;
import com.guet.issue.beans.entity.Issue;
import com.guet.issue.beans.vo.IssueDateVO;
import com.guet.issue.mapper.IssueMapper;
import com.guet.issue.service.IIssueService;
import com.guet.upsum.beans.vo.UpsumVO;
import com.guet.upsum.service.IUpsumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Li
 * @create 2022-12-16-21:45
 */
@Service
public class IssueServiceImpl implements IIssueService {

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    ICommentService commentService;

    @Autowired
    IUpsumService upsumService;

    @Override
    public void add(String newIssue, Integer userId) {
        issueMapper.add(newIssue, userId);
    }

    @Override
    public void delete(Integer id) {
        issueMapper.delete(id);
    }

    @Override
    public PageDataVO<Issue> select(Integer userId, String keyWord, Integer pageSize, Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        List<Issue> list = issueMapper.find(userId, keyWord);
        // 包装分页数据
        PageInfo<Issue> page = new PageInfo<>(list);
        PageDataVO<Issue> myPage = new PageDataVO<>();
        myPage.setItems(page.getList());
        myPage.setPageNum((long) page.getPageNum());
        myPage.setPageSize((long) page.getPageSize());
        myPage.setTotalPage((long) page.getPages());
        myPage.setTotalItem(page.getTotal());

        return myPage;
    }

    @Override
    public void updateSum(int i, Integer issueId) {
        issueMapper.updateSum(i, issueId);
    }

    @Override
    public Integer selectSum(Integer issueId) {

        return issueMapper.selectSum(issueId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IssueDateVO selectIssueAndCommentAndUpsum(Integer userId, Integer issueId) {

        PageVO pages = new PageVO();

        pages.setPageNum(1);
        pages.setPageSize(10);

        IssueDateVO issueDateVO = issueMapper.selectIssue(issueId);

        PageDataVO<CommentDateVO> commentDateVO = commentService.page(issueId, pages);

        UpsumVO upsumVO = upsumService.select(userId, issueId);

        issueDateVO.setComment(commentDateVO);

        issueDateVO.setUpsumVO(upsumVO);






        return issueDateVO;
    }

}
